package com.bocai.dahuatech.ui.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.MyCollectionListBean;
import com.bocai.dahuatech.Bean.NewsListBean;
import com.bocai.dahuatech.Bean.SolutionListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.ui.adapter.MyCollectionAdapterHolder;
import com.bocai.dahuatech.ui.view.LoadMoreRecyclerView;
import com.bocai.dahuatech.utils.AESUtils;
import com.bocai.dahuatech.utils.SharePrefencesUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 我的收藏
 */
public class MyCollectionActivity extends BaseActivity
{

    @Bind(R.id.rv_mycollection)
    LoadMoreRecyclerView rvMyCollection;
    @Bind(R.id.swipe_mycollection)
    SwipeRefreshLayout swipeMycollection;
    private MyCollectionAdapterHolder adapter;
    int pager=1;
    MyCollectionListBean bean;
    AlertDialog dialog;
    List<MyCollectionListBean.ContentEntity> datas=new ArrayList<>();
    int clickPosition;
    @Override
    protected int contentView() {
        return R.layout.activity_my_collection;
    }

    @Override
    protected void initView()
    {
        ButterKnife.bind(this);
        initToolbar(R.string.mecollection);
        rvMyCollection.setLayoutManager(new LinearLayoutManager(this));
//        adapter=new MyCollectionAdapterHolder(MyCollectionActivity.this, datas);
//        rvMyCollection.setAdapter(adapter);
        getCollectionLsit();
        initEvent();
    }

    private void getCollectionLsit()
    {
        showLoading();
        getTestApi().getCollectionLsit(SharePrefencesUtil.getFID(this),pager,20)
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(collectionObserver);
    }
    Observer<BaseBean> collectionObserver=new Observer<BaseBean>()
    {
        @Override
        public void onCompleted()
        {
        }

        @Override
        public void onError(Throwable e)
        {
            hideLoading();
            showToast(R.string.neterror);
            swipeMycollection.setRefreshing(false);
            Log.e("error", e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean)
        {
            hideLoading();
            swipeMycollection.setRefreshing(false);
            if(baseBean.getReturnNo().equals("1"))
            {
                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result",result);
                bean=new Gson().fromJson(result,MyCollectionListBean.class);
                datas.addAll(bean.getContent());
//                adapter.notifyDataSetChanged();
                if(pager==1)
                {
                    adapter=new MyCollectionAdapterHolder(MyCollectionActivity.this, datas);
                    rvMyCollection.setAdapter(adapter);
                }else
                {
                    adapter.addDatas(bean.getContent());
                }
                if(bean.getContent().size()<20)
                {
                    rvMyCollection.notifyMoreFinish(false);
                }else
                {
                    rvMyCollection.notifyMoreFinish(true);
                }
                setAdapterEvent();
            }else
            {
                showToast(baseBean.getReturnInfo());
            }
        }
    };
    @Override
    public void onEventMainThread(MainEvent event)
    {
        super.onEventMainThread(event);
        if(event.msg.equals("goshopcart"))
        {
            finish();
        }
    }
    private void setAdapterEvent() {
        adapter.SetOnItemClickListener(new MyCollectionAdapterHolder.OnItemClickListener()
        {
            @Override
            public void onItemClick(View view, int position)
            {
                clickPosition=position;
                if(datas.size()!=0)
                {
                    Intent intent;
                    if(datas.get(position).getType()==1)
                    {
                        intent=new Intent(MyCollectionActivity.this,SolutionDetailActivity.class);
                        SolutionListBean.ContentEntity bean=new SolutionListBean.ContentEntity();
                        bean.setId(datas.get(position).getFKey());
                        bean.setName(datas.get(position).getTitle());
                        bean.setDescribe(datas.get(position).getContent());
                        bean.setImageUrl(datas.get(position).getImageUrl());
                        intent.putExtra("bean",bean);
                        intent.putExtra("type",datas.get(position).getSolutionType());
                        startActivity(intent);
                    }else  if(datas.get(position).getType()==2)
                    {
                        NewsListBean.ContentEntity bean=new NewsListBean.ContentEntity();
                        bean.setImageUrl(datas.get(position).getImageUrl());
                        bean.setTitle(datas.get(position).getTitle());
                        bean.setId(datas.get(position).getFKey());
                        bean.setContent(datas.get(position).getContent());
                        intent=new Intent(MyCollectionActivity.this,NewsDetailActivity.class);
                        intent.putExtra("bean",bean);
                        startActivity(intent);
                    }else  if(datas.get(position).getType()==3)
                    {
                        intent=new Intent(MyCollectionActivity.this,NewProductDetailActivity.class);
                        intent.putExtra("productId",Integer.parseInt(datas.get(position).getFKey()));
                        intent.putExtra("solutionId","");

                        startActivity(intent);
                    }
                }
            }
        });
        adapter.SetOnItemLongClickListener(new MyCollectionAdapterHolder.OnItemLongClickListener()
        {
            @Override
            public void onItemLongClick(View view, final int position)
            {
                clickPosition=position;
                View dialogView= LayoutInflater.from(MyCollectionActivity.this).inflate(R.layout.dialog_delete, null);
                dialog = new AlertDialog.Builder(MyCollectionActivity.this)
                        .setView(dialogView).show();
                TextView tiitle=(TextView)dialog.findViewById(R.id.tv_dialog_title);
                tiitle.setText("你确定要删除该条收藏...");
                dialog.findViewById(R.id.tv_dialog_delete).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deleteCollection(position);
                        dialog.cancel();
                    }
                });
                dialog.findViewById(R.id.tv_dialog_dismiss).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.cancel();
                    }
                });
            }
        });
    }

    /**
     * 删除收藏
     * @param position
     */
    private void deleteCollection(int position) {
        getTestApi().deleteCollection(datas.get(position).getId())
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(deleteObserver);
    }
    Observer<BaseBean> deleteObserver=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {
//            datas.remove(clickPosition);
//            adapter.notifyItemRemoved(clickPosition);
//            rvMyCollection.setItemAnimator(new DefaultItemAnimator());
        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            Log.e("error",e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean) {
            Log.e("result", baseBean.getReturnInfo());
            showToast(baseBean.getReturnInfo());
            pager = 1;
            datas.clear();
            getCollectionLsit();
        }

    };
    @Override
    protected void initEvent() {
        super.initEvent();
        swipeMycollection.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pager = 1;
                datas.clear();
                getCollectionLsit();
            }
        });
        rvMyCollection.setLoadMoreListener(new LoadMoreRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                pager++;
                getCollectionLsit();
            }
        });


    }
}
